package com.telenav;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.imageio.ImageIO;

public class TTClearCrack implements TTCrack {

	public static int DIFF_THRESHOULD = 6;

	public static boolean DEBUG = false;
	public int LEFT_RIGHT_BLACK_COUNT = 10;
	public static int SELECT_RGB = -14342093;
	public static int SELECT_COUNT = 60;
	public int last_x1 = -1;
	public int last_x2 = -1;
	public int last_y2 = -1;
	public int last_y1 = -1;

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		BufferedImage bufferedImage = ImageIO.read(new File("E:\\tt\\capture.png"));
		new TTClearCrack().crackMove(null, null, bufferedImage);
	}

	public void crackMove(Robot robot, Dimension scrSize, BufferedImage bufferedImageFile) throws Exception {
		BufferedImage bufferedImage;
		if (bufferedImageFile == null) {
			bufferedImage = robot.createScreenCapture(new Rectangle(scrSize));
		} else {
			bufferedImage = bufferedImageFile;
		}
		if (DEBUG) {
			ImageIO.write(bufferedImage, "BMP", new File("E:\\tt\\capture.bmp"));
		}

		//				int pixel = bufferedImage.getRGB(483, 766);
		//				System.out.println(pixel);
		//				System.out.println(ImgProcess.GetRValue(pixel));
		//				System.out.println(ImgProcess.GetGValue(pixel));
		//				System.out.println(ImgProcess.GetBValue(pixel));

		int x1 = 0;
		int x2 = bufferedImage.getWidth();
		int y1 = 0;
		int y2 = 0;
		int width = x2 - x1;
		int height = y2 - y1;
		int yn1 = 0;
		int yn2 = height;

		if (last_x1 != -1) {
			x1 = last_x1;
			x2 = last_x2;
			y2 = last_y2;
			y1 = last_y1;
			width = x2 - x1;
			height = y2 - y1;
			yn1 = 0;
			yn2 = height;
			yn2 = yn1 + width;
			System.out.println("x1=" + x1 + ",x2=" + x2 + ",y1=" + y1 + ",y2=" + y2);
		} else {
			//first find by SELECT_RGB
			boolean shouldBreak = false;
			for (int j = bufferedImage.getHeight() - 1; j > 0 && !shouldBreak; j--) {
				for (int i = 0; i < bufferedImage.getWidth(); i++) {
					int count = 0;
					if (bufferedImage.getRGB(i, j) == SELECT_RGB && ImgProcess.GetGray(bufferedImage, i - SELECT_COUNT, j) == 0) {
						count++;
						int k = i;
						for (; k < i + SELECT_COUNT + 10; k++) {
							if (bufferedImage.getRGB(k, j) == SELECT_RGB) {
								count++;
							}
						}
						if (count > SELECT_COUNT) {
							x1 = i;
							x2 = x1 + count * 7;
							y2 = j;
							y1 = y2 - count * 7;
							width = x2 - x1;
							height = y2 - y1;
							yn1 = 0;
							yn2 = height;
							yn2 = yn1 + width;
							System.out.println("x1=" + x1 + ",x2=" + x2 + ",y1=" + y1 + ",y2=" + y2 + ",count=" + count);

							last_x1 = x1;
							last_x2 = x2;
							last_y2 = y2;
							last_y1 = y1;
							shouldBreak = true;
							break;
						}
						i = k;
					}
				}
			}
		}
		if (y1 == 0) {
			int y = bufferedImage.getHeight() / 2;
			//find the left bound
			for (int i = 0; i < bufferedImage.getWidth(); i++) {
				int grayValue = ImgProcess.GetGray(bufferedImage, i, y);
				if (grayValue < 5) {
					int count = 0;
					for (int k = i; k < bufferedImage.getWidth(); k++) {
						int grayValueT = ImgProcess.GetGray(bufferedImage, k, y);
						if (grayValueT < 5) {
							count++;
							continue;
						} else {
							i = k;
							break;
						}
					}
					if (count > LEFT_RIGHT_BLACK_COUNT) {
						x1 = i;
						System.out.println(x1 + "," + y);
						break;
					}
				}
			}
			//find the right bound
			for (int i = bufferedImage.getWidth() - 1; i > x1; i--) {
				int grayValue = ImgProcess.GetGray(bufferedImage, i, y);
				if (grayValue < 5) {
					int count = 0;
					for (int k = i; k > x1; k--) {
						int grayValueT = ImgProcess.GetGray(bufferedImage, k, y);
						if (grayValueT < 5) {
							count++;
							continue;
						} else {
							i = k;
							break;
						}
					}
					if (count > LEFT_RIGHT_BLACK_COUNT) {
						x2 = i;
						System.out.println(x2 + "," + y);
						break;
					}
				}
			}

			//find the top bound
			for (int j = y; j > 0; j--) {
				int grayValue = ImgProcess.GetGray(bufferedImage, x1, j);
				grayValue += ImgProcess.GetGray(bufferedImage, x1 - 1, j);
				grayValue += ImgProcess.GetGray(bufferedImage, x1 - 2, j);
				grayValue /= 3;
				if (grayValue > 130) {
					y1 = j;
					System.out.println(y1);
					break;
				}
			}
			//find the bottom bound
			for (int j = y; j < bufferedImage.getHeight(); j++) {
				int grayValue = ImgProcess.GetGray(bufferedImage, x1, j);
				grayValue += ImgProcess.GetGray(bufferedImage, x1 - 1, j);
				grayValue += ImgProcess.GetGray(bufferedImage, x1 - 2, j);
				grayValue /= 3;
				if (grayValue > 130) {
					y2 = j;
					System.out.println(y2);
					break;
				}
			}
			width = x2 - x1;
			height = y2 - y1;
			BufferedImage pTo = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType());
			ImgProcess.AutoThreshold(bufferedImage, pTo);
			BufferedImage imageOut = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
			for (int j = 0; j < height; j++) {
				for (int i = 0; i < width; i++) {
					int gray = ImgProcess.GetGray(pTo, x1 + i, y1 + j);
					imageOut.setRGB(i, j, ImgProcess.getRGB(gray, gray, gray));
				}
			}
			if (DEBUG) {
				ImageIO.write(imageOut, "BMP", new File("E:\\tt\\out.bmp"));
			}
			yn1 = 0;
			yn2 = height;
			//		for (int j = height / 2; j > 0; j -= 5) {
			//			int grayValue = ImgProcess.GetGray(imageOut, x, j);
			//			if (grayValue < 30) {
			//				int count = 0;
			//				for (int k = 0; k < 40; k++) {
			//					int grayValueT = ImgProcess.GetGray(imageOut, x, j - k);
			//					if (grayValueT > 30) {
			//						break;
			//					}
			//					count++;
			//				}
			//				if (count == 40) {
			//					yn1 = j + 5;
			//					System.out.println(yn1);
			//					break;
			//				}
			//			}
			//
			//		}
			//

			//find the top bound with 5 pixels continuous black
			int hcount = 0;
			for (int j = height / 2; j > 0; j -= 5) {
				int count = 0;
				for (int i = 0; i < x2 - x1; i++) {
					int grayValueT = ImgProcess.GetGray(imageOut, i, j);
					if (grayValueT < 30) {
						count++;
					}
				}
				if (count > x2 - x1 - 5) {
					hcount++;
				} else {
					hcount = 0;
				}
				if (hcount > 5) {
					yn1 = j + 20;
					break;
				}
			}

			//		int hcount = 0;
			//		for (int j = height / 2; j < height; j++) {
			//			int count = 0;
			//			for (int i = 0; i < x2 - x1; i++) {
			//				int grayValueT = ImgProcess.GetGray(imageOut, i, j);
			//				if (grayValueT < 30) {
			//					count++;
			//				}
			//			}
			//			if (count > x2 - x1 - 5) {
			//				hcount++;
			//			} else {
			//				hcount = 0;
			//			}
			//			if (hcount > 25) {
			//				yn2 = j-15;
			//				yn1 = yn2 - width;
			//				System.out.println(yn1 + "->" + yn2);
			//				break;
			//			}
			//		}

			//		if (last_yn1 != -1 && yn1 < 80) {
			//			yn1 = last_yn1;
			//		}
			yn2 = yn1 + width;
			System.out.println(yn2);
		}
		int width1 = width;
		int height1 = width;
		if (DEBUG) {
			BufferedImage imageOut1 = new BufferedImage(width1, height1, BufferedImage.TYPE_BYTE_GRAY);
			for (int j = 0; j < height1; j++) {
				for (int i = 0; i < width1; i++) {
					int gray = ImgProcess.GetGray(bufferedImage, x1 + i, y1 + yn1 + j);
					imageOut1.setRGB(i, j, ImgProcess.getRGB(gray, gray, gray));
				}
			}
			ImageIO.write(imageOut1, "BMP", new File("E:\\tt\\out1.bmp"));
		}
		BufferedImage imageOut2 = new BufferedImage(width1, height1, bufferedImage.getType());
		for (int j = 0; j < height1; j++) {
			for (int i = 0; i < width1; i++) {
				int a = x1;
				int b = y1 + yn1;
				imageOut2.setRGB(i, j, bufferedImage.getRGB(a + i, b + j));
			}
		}
		if (DEBUG) {
			ImageIO.write(imageOut2, "BMP", new File("E:\\tt\\out2.bmp"));
		}

		RGB[][] rgbs = new RGB[7][7];
		int step = width1 / 7;
		for (int j = step / 2, ky = 0; j < height1; j += step, ky++) {
			for (int i = step / 2, kx = 0; i < width1; i += step, kx++) {
				int avgR = ImgProcess.GetRValue(imageOut2.getRGB(i, j));
				int avgG = ImgProcess.GetGValue(imageOut2.getRGB(i, j));
				int avgB = ImgProcess.GetBValue(imageOut2.getRGB(i, j));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i - 1, j - 1));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i - 1, j - 1));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i - 1, j - 1));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i + 1, j - 1));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i + 1, j - 1));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i + 1, j - 1));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i + 1, j + 1));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i + 1, j + 1));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i + 1, j + 1));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i - 1, j + 1));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i - 1, j + 1));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i - 1, j + 1));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i + 1, j));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i + 1, j));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i + 1, j));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i, j - 1));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i, j - 1));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i, j - 1));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i, j + 1));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i, j + 1));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i, j + 1));
				avgR += ImgProcess.GetRValue(imageOut2.getRGB(i - 1, j));
				avgG += ImgProcess.GetGValue(imageOut2.getRGB(i - 1, j));
				avgB += ImgProcess.GetBValue(imageOut2.getRGB(i - 1, j));
				avgR /= 9;
				avgG /= 9;
				avgB /= 9;
				rgbs[kx][ky] = new RGB(avgR, avgG, avgB);
				System.out.print(avgR + "," + avgG + "," + avgB + "\t");
			}
			System.out.print("\n");
		}
		ArrayList<String> sugArray = new ArrayList<String>();
		//y add
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 5; j++) {
				if (Math.abs(rgbs[i][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
					if (j == 4) {
						if (i == 0) {
							if (Math.abs(rgbs[i + 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i + 1) + "," + (j + 2) + ";" + rgbs[i + 1][j + 2] + ";" + rgbs[i][j]);
							}
						} else if (i == 6) {
							if (Math.abs(rgbs[i - 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i - 1) + "," + (j + 2) + ";" + rgbs[i - 1][j + 2] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i + 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i + 1) + "," + (j + 2) + ";" + rgbs[i + 1][j + 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i - 1) + "," + (j + 2) + ";" + rgbs[i - 1][j + 2] + ";" + rgbs[i][j]);
							}
						}
					} else {
						if (i == 0) {
							if (Math.abs(rgbs[i + 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i + 1) + "," + (j + 2) + ";" + rgbs[i + 1][j + 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i][j + 3].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + i + "," + (j + 3) + ";" + rgbs[i][j + 3] + ";" + rgbs[i][j]);
							}
						} else if (i == 6) {
							if (Math.abs(rgbs[i - 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i - 1) + "," + (j + 2) + ";" + rgbs[i - 1][j + 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i][j + 3].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + i + "," + (j + 3) + ";" + rgbs[i][j + 3] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i + 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i + 1) + "," + (j + 2) + ";" + rgbs[i + 1][j + 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 1][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + (i - 1) + "," + (j + 2) + ";" + rgbs[i - 1][j + 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i][j + 3].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j + 2) + ";" + i + "," + (j + 3) + ";" + rgbs[i][j + 3] + ";" + rgbs[i][j]);
							}
						}
					}
				} else if (Math.abs(rgbs[i][j + 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
					if (i == 0) {
						if (Math.abs(rgbs[i + 1][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add(i + "," + (j + 1) + ";" + (i + 1) + "," + (j + 1) + ";" + rgbs[i + 1][j + 1] + ";" + rgbs[i][j]);
						}
					} else if (i == 6) {
						if (Math.abs(rgbs[i - 1][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add(i + "," + (j + 1) + ";" + (i - 1) + "," + (j + 1) + ";" + rgbs[i - 1][j + 1] + ";" + rgbs[i][j]);
						}
					} else {
						if (Math.abs(rgbs[i + 1][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add(i + "," + (j + 1) + ";" + (i + 1) + "," + (j + 1) + ";" + rgbs[i + 1][j + 1] + ";" + rgbs[i][j]);
						} else if (Math.abs(rgbs[i - 1][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add(i + "," + (j + 1) + ";" + (i - 1) + "," + (j + 1) + ";" + rgbs[i - 1][j + 1] + ";" + rgbs[i][j]);
						}
					}

				}
			}
		}

		//x add
		for (int j = 0; j < 7; j++) {
			for (int i = 0; i < 5; i++) {
				if (Math.abs(rgbs[i + 1][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
					if (i == 4) {
						if (j == 0) {
							if (Math.abs(rgbs[i + 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j + 1) + ";" + rgbs[i + 2][j + 1] + ";" + rgbs[i][j]);
							}
						} else if (j == 6) {
							if (Math.abs(rgbs[i + 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j - 1) + ";" + rgbs[i + 2][j - 1] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i + 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j + 1) + ";" + rgbs[i + 2][j + 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j - 1) + ";" + rgbs[i + 2][j - 1] + ";" + rgbs[i][j]);
							}
						}
					} else {
						if (j == 0) {
							if (Math.abs(rgbs[i + 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j + 1) + ";" + rgbs[i + 2][j + 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 3][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 3) + "," + j + ";" + rgbs[i + 3][j] + ";" + rgbs[i][j]);
							}
						} else if (j == 6) {
							if (Math.abs(rgbs[i + 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j - 1) + ";" + rgbs[i + 2][j - 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 3][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 3) + "," + j + ";" + rgbs[i + 3][j] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i + 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j + 1) + ";" + rgbs[i + 2][j + 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 2) + "," + (j - 1) + ";" + rgbs[i + 2][j - 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 3][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i + 2) + "," + j + ";" + (i + 3) + "," + j + ";" + rgbs[i + 3][j] + ";" + rgbs[i][j]);
							}
						}
					}
				} else if (Math.abs(rgbs[i + 2][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
					if (j == 0) {
						if (Math.abs(rgbs[i + 1][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add((i + 1) + "," + j + ";" + (i + 1) + "," + (j + 1) + ";" + rgbs[i + 1][j + 1] + ";" + rgbs[i][j]);
						}
					} else if (j == 6) {
						if (Math.abs(rgbs[i + 1][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add((i + 1) + "," + j + ";" + (i + 1) + "," + (j - 1) + ";" + rgbs[i + 1][j - 1] + ";" + rgbs[i][j]);
						}
					} else {
						if (Math.abs(rgbs[i + 1][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add((i + 1) + "," + j + ";" + (i + 1) + "," + (j + 1) + ";" + rgbs[i + 1][j + 1] + ";" + rgbs[i][j]);
						} else if (Math.abs(rgbs[i + 1][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
							sugArray.add((i + 1) + "," + j + ";" + (i + 1) + "," + (j - 1) + ";" + rgbs[i + 1][j - 1] + ";" + rgbs[i][j]);
						}
					}

				}
			}
		}
		//y sub
		for (int i = 6; i > -1; i--) {
			for (int j = 6; j > 1; j--) {
				if (Math.abs(rgbs[i][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
					if (j == 2) {
						if (i == 6) {
							if (Math.abs(rgbs[i - 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i - 1) + "," + (j - 2) + ";" + rgbs[i - 1][j - 2] + ";" + rgbs[i][j]);
							}
						} else if (i == 0) {
							if (Math.abs(rgbs[i + 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i + 1) + "," + (j - 2) + ";" + rgbs[i + 1][j - 2] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i - 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i - 1) + "," + (j - 2) + ";" + rgbs[i - 1][j - 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i + 1) + "," + (j - 2) + ";" + rgbs[i + 1][j - 2] + ";" + rgbs[i][j]);
							}
						}
					} else {
						if (i == 6) {
							if (Math.abs(rgbs[i - 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i - 1) + "," + (j - 2) + ";" + rgbs[i - 1][j - 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i][j - 3].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + i + "," + (j - 3) + ";" + rgbs[i][j - 3] + ";" + rgbs[i][j]);
							}
						} else if (i == 0) {
							if (Math.abs(rgbs[i + 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i + 1) + "," + (j - 2) + ";" + rgbs[i + 1][j - 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i][j - 3].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + i + "," + (j - 3) + ";" + rgbs[i][j - 3] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i - 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i - 1) + "," + (j - 2) + ";" + rgbs[i - 1][j - 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i + 1][j - 2].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + (i + 1) + "," + (j - 2) + ";" + rgbs[i + 1][j - 2] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i][j - 3].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add(i + "," + (j - 2) + ";" + i + "," + (j - 3) + ";" + rgbs[i][j - 3] + ";" + rgbs[i][j]);
							}
						}
					}
				}
			}
		}

		//x sub
		for (int j = 6; j > -1; j--) {
			for (int i = 6; i > 1; i--) {
				if (Math.abs(rgbs[i - 1][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
					if (i == 2) {
						if (j == 6) {
							if (Math.abs(rgbs[i - 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j - 1) + ";" + rgbs[i - 2][j - 1] + ";" + rgbs[i][j]);
							}
						} else if (j == 0) {
							if (Math.abs(rgbs[i - 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j + 1) + ";" + rgbs[i - 2][j + 1] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i - 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j - 1) + ";" + rgbs[i - 2][j - 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j + 1) + ";" + rgbs[i - 2][j + 1] + ";" + rgbs[i][j]);
							}
						}
					} else {
						if (j == 6) {
							if (Math.abs(rgbs[i - 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j - 1) + ";" + rgbs[i - 2][j - 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 3][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 3) + "," + j + ";" + rgbs[i - 3][j] + ";" + rgbs[i][j]);
							}
						} else if (j == 0) {
							if (Math.abs(rgbs[i - 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j + 1) + ";" + rgbs[i - 2][j + 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 3][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 3) + "," + j + ";" + rgbs[i - 3][j] + ";" + rgbs[i][j]);
							}
						} else {
							if (Math.abs(rgbs[i - 2][j - 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j - 1) + ";" + rgbs[i - 2][j - 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 2][j + 1].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 2) + "," + (j + 1) + ";" + rgbs[i - 2][j + 1] + ";" + rgbs[i][j]);
							} else if (Math.abs(rgbs[i - 3][j].sub(rgbs[i][j])) < DIFF_THRESHOULD) {
								sugArray.add((i - 2) + "," + j + ";" + (i - 3) + "," + j + ";" + rgbs[i - 3][j] + ";" + rgbs[i][j]);
							}
						}
					}
				}
			}
		}

		ArrayList<String> positions = new ArrayList<String>();
		for (String str : sugArray) {
			String[] strs = str.split(";");
			StringBuffer p = new StringBuffer();
			int bx1 = Integer.valueOf(strs[1].split(",")[0]);
			int by1 = Integer.valueOf(strs[1].split(",")[1]);
			int bx0 = Integer.valueOf(strs[0].split(",")[0]);
			int by0 = Integer.valueOf(strs[0].split(",")[1]);
			p.append((x1 + step * bx1 + step / 2) + "," + (y1 + yn1 + by1 * step + step / 2) + "->");
			p.append((x1 + step * bx0 + step / 2) + "," + (y1 + yn1 + by0 * step + step / 2));
			positions.add(p.toString());
			System.out.println(str + "\t" + p.toString());

		}

		for (String p : positions) {
			int cx2 = Integer.valueOf(p.split("->")[0].split(",")[0]);
			int cx1 = Integer.valueOf(p.split("->")[1].split(",")[0]);
			int cy2 = Integer.valueOf(p.split("->")[0].split(",")[1]);
			int cy1 = Integer.valueOf(p.split("->")[1].split(",")[1]);
			if (cy1 < y1 || cy1 > y2 || cy2 < y1 || cy2 > y2) {
				continue;
			}
			if (robot != null) {
				robot.mouseMove(cx2, cy2);
				robot.mousePress(InputEvent.BUTTON1_MASK);
				robot.delay(5);
				robot.mouseMove(cx1, cy1);
				robot.mouseRelease(InputEvent.BUTTON1_MASK);
				robot.delay(5);
			}
			for (int i = cx1; i < cx1 + 10; i++) {
				for (int j = cy1; j < cy1 + 10; j++) {
					bufferedImage.setRGB(i, j, ImgProcess.getRGB(255, 0, 0));
				}
			}
			for (int i = cx2; i < cx2 + 10; i++) {
				for (int j = cy2; j < cy2 + 10; j++) {
					bufferedImage.setRGB(i, j, ImgProcess.getRGB(0, 255, 0));
				}
			}

		}

		if (robot != null) {
			Iterator<Entry<String, Long>> it = userInputMap.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, Long> entry = it.next();
				if (entry.getKey().equals("click")) {
					Long bx = entry.getValue();
					for (int by = 0; by < 7; by++) {
						int cx = x1 + step * bx.intValue() + step / 2;
						int cy = y1 + yn1 + by * step + step / 2;
						if (cy < y1 || cy > y2) {
							continue;
						}
						robot.mouseMove(cx, cy);
						robot.mousePress(InputEvent.BUTTON1_MASK);
						robot.mouseRelease(InputEvent.BUTTON1_MASK);
						robot.delay(5);
					}
					it.remove();
				}
			}
		}
		if (DEBUG) {
			ImageIO.write(bufferedImage, "BMP", new File("E:\\tt\\out3.bmp"));
		}
	}
}
